package house

import "fmt"

type HouseLogic struct {
	dao *HouseDao
}

func NewHouseLogic() *HouseLogic {
	return &HouseLogic{
		dao: NewHouseDao(),
	}
}
func (h *HouseLogic) houseInfo(userId string) (House, error) {
	return h.dao.HouserInfo(userId)
}
func (h *HouseLogic) unLockHouse(userId string, houseId int32) (House, error) {
	userHouse, _ := h.dao.HouserInfo(userId)
	_, ok := userHouse.HouseList[houseId]
	if ok {
		return userHouse, fmt.Errorf("不要")
	}
	houseInfo := HouseInfo{
		HouseId:       houseId,
		FurnitureList: map[int32]FurnitureInfo{},
	}
	return h.dao.updateHouseInfo(userId, houseId, houseInfo)
}
func (h *HouseLogic) unlockFurniture(userId string, houseId, furnitureId int32) (House, error) {
	userHouse, _ := h.dao.HouserInfo(userId)
	houseInfo, ok := userHouse.HouseList[houseId]
	if !ok {
		return userHouse, fmt.Errorf("没有该数据")
	}
	_, ok = houseInfo.FurnitureList[furnitureId]
	if ok {
		return userHouse, fmt.Errorf("已经解锁了，不能解锁")
	}
	var furnitureInfo = FurnitureInfo{
		FurnitureId: furnitureId,
		IsOn:        false,
	}
	return h.dao.updateFurnitureInfo(userId, houseId, furnitureInfo)
}
func (h *HouseLogic) addFurniture(userId string, houseId, furnitureId int32) (House, error) {
	info, err := h.dao.furnitureInfoByHouseId(userId, houseId, furnitureId)
	if err != nil {
		return House{}, err
	}
	info.IsOn = true
	return h.dao.updateFurnitureInfo(userId, houseId, info)
}
func (h *HouseLogic) computation(userId string) {

}
func (h *HouseLogic) initHouse(userId string, houseId int32) (House, error) {
	_, err := h.dao.HouserInfo(userId)
	if err == nil {
		return House{}, fmt.Errorf("已经初始化，不能重复")
	}
	houseInfo := HouseInfo{
		HouseId:       houseId,
		FurnitureList: make(map[int32]FurnitureInfo, 0),
	}
	houseList := House{
		UserId:    userId,
		HouseList: map[int32]HouseInfo{houseId: houseInfo},
	}
	err = h.dao.Insert(houseList)
	if err != nil {
		return House{}, err
	}
	return houseList, nil
}
